<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/wiggly.qdoc -->
<head>
  <title>Qt 4.3: Wiggly Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Wiggly Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="widgets-wiggly-dialog-cpp.html">widgets/wiggly/dialog.cpp</a></li>
<li><a href="widgets-wiggly-dialog-h.html">widgets/wiggly/dialog.h</a></li>
<li><a href="widgets-wiggly-wigglywidget-cpp.html">widgets/wiggly/wigglywidget.cpp</a></li>
<li><a href="widgets-wiggly-wigglywidget-h.html">widgets/wiggly/wigglywidget.h</a></li>
<li><a href="widgets-wiggly-main-cpp.html">widgets/wiggly/main.cpp</a></li>
</ul>
<p>The Wiggly example shows how to animate a widget using <a href="qbasictimer.html">QBasicTimer</a> and <a href="qobject.html#timerEvent">timerEvent()</a>. In addition, the example demonstrates how to use <a href="qfontmetrics.html">QFontMetrics</a> to determine the size of text on screen.</p>
<p align="center"><img src="images/wiggly-example.png" alt="Screenshot of the Wiggly example" /></p><p><a href="qbasictimer.html">QBasicTimer</a> is a low-level class for timers. Unlike <a href="qtimer.html">QTimer</a>, <a href="qbasictimer.html">QBasicTimer</a> doesn't inherit from <a href="qobject.html">QObject</a>; instead of emitting a <a href="qtimer.html#timeout">timeout()</a> signal when a certain amount of time has passed, it sends a <a href="qtimerevent.html">QTimerEvent</a> to a <a href="qobject.html">QObject</a> of our choice. This makes <a href="qbasictimer.html">QBasicTimer</a> a more lightweight alternative to <a href="qtimer.html">QTimer</a>. Qt's built-in widgets use it internally, and it is provided in Qt's API for highly-optimized applications (e.g&#x2e;, <a href="qtopiacore.html">Qtopia Core</a> applications).</p>
<p>The example consists of two classes:</p>
<ul>
<li><tt>WigglyWidget</tt> is the custom widget displaying the text in a wiggly line.</li>
<li><tt>Dialog</tt> is the dialog widget allowing the user to enter a text. It combines a <tt>WigglyWidget</tt> and a <tt>QLineEdit</tt>.</li>
</ul>
<p>We will first take a quick look at the <tt>Dialog</tt> class, then we will review the <tt>WigglyWidget</tt> class.</p>
<a name="dialog-class-definition"></a>
<h2>Dialog Class Definition</h2>
<pre> class Dialog : public QDialog
 {
     Q_OBJECT

 public:
     Dialog(QWidget *parent = 0);
 };</pre>
<p>The <tt>Dialog</tt> class provides a dialog widget that allows the user to enter a text. The text is then rendered by <tt>WigglyWidget</tt>.</p>
<a name="dialog-class-implementation"></a>
<h2>Dialog Class Implementation</h2>
<pre> Dialog::Dialog(QWidget *parent)
     : QDialog(parent)
 {
     WigglyWidget *wigglyWidget = new WigglyWidget;
     QLineEdit *lineEdit = new QLineEdit;

     QVBoxLayout *layout = new QVBoxLayout;
     layout-&gt;addWidget(wigglyWidget);
     layout-&gt;addWidget(lineEdit);
     setLayout(layout);

     connect(lineEdit, SIGNAL(textChanged(QString)),
             wigglyWidget, SLOT(setText(QString)));

     lineEdit-&gt;setText(tr(&quot;Hello world!&quot;));

     setWindowTitle(tr(&quot;Wiggly&quot;));
     resize(360, 145);
 }</pre>
<p>In the constructor we create a wiggly widget along with a <a href="qlineedit.html">line edit</a>, and we put the two widgets in a vertical layout. We connect the line edit's <a href="qlineedit.html#textChanged">textChanged()</a> signal to the wiggly widget's <tt>setText()</tt> slot to obtain the real time interaction with the wiggly widget. The widget's default text is &quot;Hello world!&quot;.</p>
<a name="wigglywidget-class-definition"></a>
<h2>WigglyWidget Class Definition</h2>
<pre> class WigglyWidget : public QWidget
 {
     Q_OBJECT

 public:
     WigglyWidget(QWidget *parent = 0);

 public slots:
     void setText(const QString &amp;newText) { text = newText; }

 protected:
     void paintEvent(QPaintEvent *event);
     void timerEvent(QTimerEvent *event);

 private:
     QBasicTimer timer;
     QString text;
     int step;
 };</pre>
<p>The <tt>WigglyWidget</tt> class provides the wiggly line displaying the text. We subclass <a href="qwidget.html">QWidget</a> and reimplement the standard <a href="qwidget.html#paintEvent">paintEvent()</a> and <a href="qobject.html#timerEvent">timerEvent()</a> functions to draw and update the widget. In addition we implement a public <tt>setText()</tt> slot that sets the widget's text.</p>
<p>The <tt>timer</tt> variable, of type <a href="qbasictimer.html">QBasicTimer</a>, is used to update the widget at regular intervals, making the widget move. The <tt>text</tt> variable is used to store the currently displayed text, and <tt>step</tt> to calculate position and color for each character on the wiggly line.</p>
<a name="wigglywidget-class-implementation"></a>
<h2>WigglyWidget Class Implementation</h2>
<pre> WigglyWidget::WigglyWidget(QWidget *parent)
     : QWidget(parent)
 {
     setBackgroundRole(QPalette::Midlight);
     setAutoFillBackground(true);

     QFont newFont = font();
     newFont.setPointSize(newFont.pointSize() + 20);
     setFont(newFont);

     step = 0;
     timer.start(60, this);
 }</pre>
<p>In the constructor, we make the widget's background slightly lighter than the usual background using the <a href="qpalette.html#ColorRole-enum">QPalette::Midlight</a> color role. The background role defines the brush from the widget's palette that Qt uses to paint the background. Then we enlarge the widget's font with 20 points.</p>
<p>Finally we start the timer; the call to <a href="qbasictimer.html#start">QBasicTimer::start</a>() makes sure that <i>this</i> particular wiggly widget will receive the timer events generated when the timer times out (every 60 milliseconds).</p>
<pre> void WigglyWidget::paintEvent(QPaintEvent * <span class="comment">/* event */</span>)
 {
     static const int sineTable[16] = {
         0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38
     };

     QFontMetrics metrics(font());
     int x = (width() - metrics.width(text)) / 2;
     int y = (height() + metrics.ascent() - metrics.descent()) / 2;
     QColor color;</pre>
<p>The <tt>paintEvent()</tt> function is called whenever a <a href="qpaintevent.html">QPaintEvent</a> is sent to the widget. Paint events are sent to widgets that need to update themselves, for instance when part of a widget is exposed because a covering widget was moved. For the wiggly widget, a paint event will also be generated every 60 milliseconds from the <tt>timerEvent()</tt> slot.</p>
<p>The <tt>sineTable</tt> represents y-values of the sine curve, multiplied by 100. It is used to make the wiggly widget move along the sine curve.</p>
<p>The <a href="qfontmetrics.html">QFontMetrics</a> object provides information about the widget's font. The <tt>x</tt> variable is the horizontal position where we start drawing the text. The <tt>y</tt> variable is the vertical position of the text's base line. Both variables are computed so that the text is horizontally and vertically centered. To compute the base line, we take into account the font's ascent (the height of the font above the base line) and font's descent (the height of the font below the base line). If the descent equals the ascent, they cancel out each other and the base line is at <tt>height()</tt> / 2.</p>
<pre>     QPainter painter(this);
     for (int i = 0; i &lt; text.size(); ++i) {
         int index = (step + i) % 16;
         color.setHsv((15 - index) * 16, 255, 191);
         painter.setPen(color);
         painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400),
                          QString(text[i]));
         x += metrics.width(text[i]);
     }
 }</pre>
<p>Each time the <tt>paintEvent()</tt> function is called, we create a <a href="qpainter.html">QPainter</a> object <tt>painter</tt> to draw the contents of the widget. For each character in <tt>text</tt>, we determine the color and the position on the wiggly line based on <tt>step</tt>. In addition, <tt>x</tt> is incremented by the character's width.</p>
<p>For simplicity, we assume that QFontMetrics::width(<tt>text</tt>) returns the sum of the individual character widths (QFontMetrics::width(<tt>text[i]</tt>)). In practice, this is not always the case because QFontMetrics::width(<tt>text</tt>) also takes into account the kerning between certain letters (e.g&#x2e;, 'A' and 'V'). The result is that the text isn't perfectly centered. You can verify this by typing &quot;AVAVAVAVAVAV&quot; in the line edit.</p>
<pre> void WigglyWidget::timerEvent(QTimerEvent *event)
 {
     if (event-&gt;timerId() == timer.timerId()) {
         ++step;
         update();
     } else {
         QWidget::timerEvent(event);
     }</pre>
<p>The <tt>timerEvent()</tt> function receives all the timer events that are generated for this widget. If a timer event is sent from the widget's <a href="qbasictimer.html">QBasicTimer</a>, we increment <tt>step</tt> to make the text move, and call <a href="qwidget.html#update">QWidget::update</a>() to refresh the display. Any other timer event is passed on to the base class's implementation of the <a href="qobject.html#timerEvent">timerEvent()</a> function.</p>
<p>The <a href="qwidget.html#update">QWidget::update</a>() slot does not cause an immediate repaint; instead the slot schedules a paint event for processing when Qt returns to the main event loop. The paint events are then handled by <tt>WigglyWidget</tt>'s <tt>paintEvent()</tt> function.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
